export interface Source {
  hostname: string;
  level: string;
  ip: string;
  time: string;
  info: string;
}

export interface DataResort {
  _index: string;
  _type: string;
  _id: string;
  _score?: any;
  _source: Source;
  sort: number[];
}

function dataResort(data: { date: string; dateData: string }[]) {
  let newArr: { date: string; subList: Array<Object> }[] = [];
  data.forEach((item, i) => {
    //NOTE 默认当前操作的数据下标 -1
    let index = -1;
    let isExists = newArr.some((newItem, j) => {
      if (item.date == newItem.date) {
        //NOTE 存在就保存当前数据下标  用于插入数据
        index = j;
        return true;
      }
    });
    if (!isExists) {
      newArr.push({
        date: item.date,
        subList: [item],
      });
    } else {
      newArr[index].subList.push(item);
    }
  });

  return newArr;
}

function dataFilter(oldData: DataResort[]) {
  const newData: { date: string; dateData: string }[] = [];
  oldData.forEach((item) => {
    const date = new Date(item.sort[0]);
    let Y = date.getFullYear();
    let M =
      date.getMonth() + 1 < 10
        ? "0" + (date.getMonth() + 1)
        : date.getMonth() + 1;
    let D = date.getDate();
    let h = date.getHours();
    let m = date.getMinutes();
    let s = date.getSeconds();
    let H = "";
    if (h >= 0 && h < 12) {
      H = "00:00";
    } else H = "12:00";
    newData.push({
      date: `${Y}-${M}-${D} ${H}`,
      dateData: `${Y}-${M}-${D} ${h}:${m}:${s}`,
    });
  });

  return newData;
}